package com.shareware.common.memory.entity

import android.os.Process
import com.shareware.common.utils.ActivityExt
import com.shareware.common.utils.appBackground
import com.shareware.common.utils.getCurProcessName
import com.shareware.common.utils.safeLet
import org.json.JSONObject

/**
 * desc: 功能描述
 * email: mobiledeveloper@qq.com
 */
data class ProcessInfo(
    val pid: Int = Process.myPid(),
    val name: String = getCurProcessName(),
    val activity: String = ActivityExt.getTopActivity()?.javaClass?.name ?: "",
    val isAppFg: Boolean = appBackground,
) {
    override fun toString(): String {
        return String.format(
            "%-21s\t%-21s %-21s",
            name,
            "Activity=$activity",
            "AppForeground=$isAppFg"
        )
    }

    fun toJson() = safeLet(defVal = JSONObject()) {
        JSONObject().apply {
            put("pid", pid)
            put("name", name)
            put("activity", activity)
            put("isAppFg", isAppFg)
        }
    }
}